//
//  DuoBaoHisTab.m
//  夺宝吧
//
//  Created by xiaocai on 15/8/19.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "DuoBaoHisTab.h"
#import "HisAllCell.h"
#import "HisRltCell.h"
#import "MJRefresh.h"
#import "PdcDetailVC.h"
#import "RecordHisModel.h"
#define DUOBAO_RECOARD_URL @"/api/ApiGoods/getAllShoppingList"
#import "NoDataView.h"
@interface DuoBaoHisTab ()<UITableViewDataSource,UITableViewDelegate>
{
    //数据源
    NSMutableArray *_data;
    NSInteger _currentPage;
}
@end
@implementation DuoBaoHisTab

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _data = [NSMutableArray array];
        _currentPage = 1;
        //注册cell
        [self registerNib:[UINib nibWithNibName:@"HisAllCell" bundle:nil] forCellReuseIdentifier:@"allcell"];
        [self registerNib:[UINib nibWithNibName:@"HisRltCell" bundle:nil] forCellReuseIdentifier:@"rltcell"];
        self.dataSource = self;
        self.delegate = self;
        self.tableFooterView = [[UIView alloc]init];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self setupRefresh];
    }
    return self;
}
- (void)setUserId:(NSString *)userId
{
    _userId = userId;
    [self startRequestForData];
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    //    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // dateKey用于存储刷新时间，可以保证不同界面拥有不同的刷新时间
    [self addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    _currentPage = 1;
    [_data removeAllObjects];
    [self startRequestForData];
}

- (void)footerRereshing
{
    _currentPage++;
    [self startRequestForData];
}
//启动数据请求
- (void)startRequestForData
{
    [WYPublic addHudToWindow];
    NSDictionary *params = nil;
    if (self.isMyRecordFlag) {
        params = @{@"uid":[UserInfo getUserID],@"filter":[NSString stringWithFormat:@"%ld",(unsigned long)self.tag],@"num":@"20",@"page":[NSString stringWithFormat:@"%ld",(unsigned long)_currentPage]};
    }else{
        params = @{@"uid":_userId,@"filter":[NSString stringWithFormat:@"%ld",(unsigned long)self.tag],@"num":@"20",@"page":[NSString stringWithFormat:@"%ld",(unsigned long)_currentPage]};
    }
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,DUOBAO_RECOARD_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSArray *list = dic[@"data"];
        for (NSDictionary *record in list) {
            RecordHisModel *model = [[RecordHisModel alloc]init];
            model.is_locked = record[@"is_locked"];
            model.his_record_id = record[@"id"];
            model.his_record_sid = record[@"sid"];
            model.his_record_code = record[@"code"];
            model.his_record_danjia = record[@"yunjiage"];
            model.his_record_gonumber = record[@"gonumber"];
            model.his_record_imgPath = record[@"thumb_url"];
            model.his_record_moneycount = record[@"moneycount"];
            NSArray *nums = record[@"goucode"];
            model.his_record_nums = [[NSMutableArray alloc]initWithArray:nums];
            model.his_record_qishu = record[@"qishu"];
            model.his_record_time = record[@"time"];
            model.his_record_title = record[@"title"];
            model.his_all_need = [WYPublic getUsefullStr:record[@"zongrenshu"]];
            model.his_left_need = [WYPublic getUsefullStr:record[@"shenyurenshu"]];
            model.his_statu = [WYPublic getUsefullStr:record[@"goods_status"]];
            
            model.his_owner_code = [WYPublic getUsefullStr:record[@"q_user_code"]];
            model.his_owner_count = [WYPublic getUsefullStr:record[@"q_gonumber"]];
            model.his_owner_name = [WYPublic getUsefullStr:record[@"q_username"]];
            model.his_owner_time = [WYPublic getUsefullStr:record[@"q_end_time"]];
            
            NSNumber *flagNum = record[@"goods_status"];
            if ([flagNum integerValue] == 1) {
                model.isGoOnFlag = YES;
            }else{
                model.isGoOnFlag = NO;
            }
            
            if (self.isMyRecordFlag) {
                model.isMineFlag = YES;
            }else{
                model.isMineFlag = NO;
            }
            [_data addObject:model];
        }
        if (_data.count == 0) {
            [self addNODataView];
        }
        [self reloadData];
        [self headerEndRefreshing];
        [self footerEndRefreshing];
        [WYPublic removeHudToWindow];
    } failedBlock:^{
        [self headerEndRefreshing];
        [self footerEndRefreshing];
        [WYPublic removeHudToWindow];
    } needLog:NO];
}
//没有数据默认填充的控件
- (void)addNODataView{
    NoDataView *nodateView = [[[NSBundle mainBundle]loadNibNamed:@"NoDataView" owner:self options:nil]lastObject];
    nodateView.frame = CGRectMake(0, 64, WYMWITH, self.contentSize.height);
    if (self.isMyRecordFlag) {
        nodateView.isMineFlag = YES;
        nodateView.title = @"您还没有夺宝记录哦!";
    }else{
        nodateView.isMineFlag = NO;
        nodateView.title = @"ta还没有夺宝记录哦!";
    }
    [self addSubview:nodateView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < _data.count) {
        RecordHisModel *model = _data[indexPath.row];
        if ([model.his_statu intValue] == 3){
            return 165.0;
        }
    }
    return 110.0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *c = [[UITableViewCell alloc]init];
    if (indexPath.row >= _data.count) {
        return c;
    }
    RecordHisModel *model = _data[indexPath.row];
    if ([model.his_statu integerValue] == 3) {
        HisRltCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rltcell"];
        [cell fillCellWithModel:model];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
        }else{
            HisAllCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allcell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fillCellWithModel:model];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
    }
    
    
    return c;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_data.count > 0) {
        RecordHisModel *model = _data[indexPath.row];
        //跳往商品详情
        PdcDetailVC *detailVC = [[PdcDetailVC alloc]init];
        detailVC.pdc_id = model.his_record_id;
        [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:detailVC animated:YES];
    }
}
@end
